package com.jmag.nop.service.common.api;

/**
 * @ClassName ApiParam
 * @Description TODO
 * @Author JMAG
 * @Version 1.0
 **/
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.jmag.nop.service.common.param.Param;
import com.jmag.nop.service.common.param.ParamNames;
import org.apache.commons.lang.StringUtils;

public class ApiParam extends JSONObject implements Param {
    private static final long serialVersionUID = 6718200590738465201L;
    private boolean ignoreSign;
    private boolean ignoreValidate;
    private String restName;
    private String restVersion;

    public ApiParam(Map<String, Object> map) {
        super(map);
    }

    public String fatchSignAndRemove() {
        String sign = this.fatchSign();
        this.remove(ParamNames.SIGN_NAME);
        return sign;
    }

    public String fatchName() {
        String name = this.getString(ParamNames.API_NAME);
        if (name == null) {
            name = this.restName;
        }

        return name;
    }

    public void setName(String name) {
        this.restName = name;
    }


    public static String buildNameVersion(String name, String version) {
        return StringUtils.isEmpty(version) ? name : name + version;
    }

    public String fatchVersion() {
        String version = this.getString(ParamNames.VERSION_NAME);
        if (version == null) {
            version = this.restVersion;
        }

        return version;
    }

    public void setVersion(String version) {
        this.restVersion = version;
    }

    public String fatchAppKey() {
        return this.getString(ParamNames.APP_KEY_NAME);
    }

    public String fatchData() {
        return this.getString(ParamNames.DATA_NAME);
    }

    public void setData(String json) {
        this.put(ParamNames.DATA_NAME, json);
    }

    public String fatchTimestamp() {
        return this.getString(ParamNames.TIMESTAMP_NAME);
    }

    public void setTimestamp(String timestamp) {
        this.put(ParamNames.TIMESTAMP_NAME, timestamp);
    }

    public String fatchSign() {
        return this.getString(ParamNames.SIGN_NAME);
    }

    public void setSign(String sign) {
        this.put(ParamNames.SIGN_NAME, sign);
    }

    public String fatchFormat() {
        String format = this.getString(ParamNames.FORMAT_NAME);
        return format != null && !"".equals(format) ? format : "json";
    }

    public void setFormat(String format) {
        this.put(ParamNames.FORMAT_NAME, format);
    }

    public String fatchAccessToken() {
        return this.getString(ParamNames.ACCESS_TOKEN_NAME);
    }

    public String fatchSignMethod() {
        String signMethod = this.getString(ParamNames.SIGN_METHOD_NAME);
        return signMethod == null ? "md5" : signMethod;
    }

    public ApiParam clone() {
        ApiParam param = new ApiParam(this);
        param.ignoreSign = this.ignoreSign;
        param.ignoreValidate = this.ignoreValidate;
        return param;
    }
}
